﻿Ext.define('I2R.view.myproposals', {
    extend: 'Ext.Panel',
    alias: 'widget.i2rmyproposals',
    border: false,
    layout: 'border',
    bodyCls: 'padd-up',
    initComponent: function () {

        var me = this;
        var store = Ext.data.StoreManager.lookup('proposals');
        
        store.load();

        var myTreeStore = Ext.create('Ext.data.TreeStore', {

            model: 'I2R.model.proposaltreeitem',
            proxy: {
                type: 'ajax',
                url: '../Proposal/MyProposals',
                reader: {
                    record: 'proposaltreeitem',
                    idProperty: 'id',
                    type: 'xml' 
                }
            },

            listeners: {

                // Each demo.UserModel instance will be automatically 
                // decorated with methods/properties of Ext.data.NodeInterface 
                // (i.e., a "node"). Whenever a UserModel node is appended
                // to the tree, this TreeStore will fire an "append" event.
                append: function (thisNode, newChildNode, index, eOpts) {

                    // If the node that's being appended isn't a root node, then we can 
                    // assume it's one of our UserModel instances that's been "dressed 
                    // up" as a node
                    if (!newChildNode.isRoot()) {
                        newChildNode.set('leaf', true);
                    }
                }
            }
        });

//        userTreeStore.setRootNode({
//            text: 'Users',
//            leaf: false,
//            expanded: false // If this were true, the store would load itself 
//            // immediately; we do NOT want that to happen
//        });

        me.items = [
            {
                id: 'proposals-tree',
                xtype: 'treepanel',
                region: 'west',
                title: 'My deviations',
                width: 200,
                rootVisible: false,
                store: myTreeStore,
//                Ext.create('Ext.data.TreeStore', {
//                    root: {
//                        expanded: true,
//                        children: [
//                            { text: 'All deviations (6)', leaf: true, id: 'All' },
//                            { text: 'Drafts (0)', leaf: true, id: 'Draft' },
//                            { text: 'Unhandled (4)', leaf: true, id: 'Unhandled' },
//                            { text: 'Postponed (2)', leaf: true, id: 'Postponed' },
//                        ]
//                    }
//                })
                },
            {
                id: 'my-proposals-grid',
                xtype: 'grid',
                region: 'center',
                store: store,
                margins: '0 0 0 4',
                title: 'All deviations',
                columns: [
                    {
                        header: 'Pr-Nr',
                        dataIndex: 'Id',
                        width: 50
                    },
                    {
                        header: 'Subject',
                        dataIndex: 'Subject',
                        flex: 1
                    },
                    {
                        header: 'Current state',
                        dataIndex: 'CurrentState',
                        width: 100
                    },
                    {
                        header: 'Updated at',
                        dataIndex: 'Updated',
                        width: 80
                    }
                ],

                dockedItems: [{
                    id: 'my-proposals-grid-pager',
                    xtype: 'pagingtoolbar',
                    // store: store,   // same store GridPanel is using
                    dock: 'bottom',
                    displayInfo: true
                }],
                viewConfig: {
                    emptyText: '<div class="empty-grid-message">No deviations.</div>',
                    deferEmptyText: false
                }
            }
        ];

        me.callParent(arguments);

    }
});